from google.appengine.api import users
from gtug.models import User

class DummyUser:
    
    user = None
    profile = {}
    
    def __init__(self, user, profile):
        self.user = user
        self.profile = profile
        
    def is_authenticated(self):
        if self.profile['id']==-1:
            return False
        else:
            return True

class AuthenticationMiddleware(object):

    def process_request(self, request):
        user = users.GetCurrentUser()
        if user:
            
            profile = User.get_profile_by_email( user.email() )
            
            if profile['id']==-1:
                u = DummyUser(user, {'name': 'LOGGED_NO_PERM', 'id': 0})
            else:
                u = DummyUser(user, profile)
                
            request.user = u
        else:
            # nao loga
            request.user = DummyUser(None, {'name': 'NOT_LOGGED', 'id': -1})
            
        return None